package com.lp.gps_alarm;

//import java.util.List;

import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * Activity um Weckpoint auszulesen.
 * 
 * @author Jonathan Durst
 * @version 1.0
 */

public class Set_Point_GPS_Alarm extends MapActivity {

	private Button home;
	private MapController mc;
	private GeoPoint p;
	private EditText radius;
	private EditText descr;
	private Button saveButton;

	private LocationManager manager = null;
	private LocationListener locationListener = null;

	private AttributeHandler_GPS_Alarm ah = AttributeHandler_GPS_Alarm
			.getInstance();

	/**
	 * verlinkt Home-Button mit Home-Activity
	 * 
	 * speichert wenn Save-Button klickt die Parameter der Map, Descriptionfeld
	 * und Radiusfeld in den AttributeHandler
	 * 
	 * @param savedInstanceState
	 *            Standardparameter
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setringpoint);

		// initializieren
		createLocationListener();
		manager = (LocationManager) getSystemService(LOCATION_SERVICE);

		// Koordinaten auslesen
		List<String> providers = manager.getAllProviders();
		for (String providerString : providers) {
			// System.out.println("Wir haben einen Provider " + providerString);
			manager.requestLocationUpdates(providerString, 60000, 0,
					locationListener);
			Location location = manager.getLastKnownLocation(providerString);
			if (null != location) {
				ah.setxCoordinate(location.getLatitude());
				ah.setyCoordinate(location.getLongitude());
				break;
			} else {
				ah.setxCoordinate(47.668025); // setze default Koordinaten Htwg
				ah.setyCoordinate(9.170988); // setze default Koordinaten Htwg
			}
		}
		manager.removeUpdates(locationListener);

		home = (Button) findViewById(R.id.homeButtonId);
		home.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent newIntent = new Intent(Set_Point_GPS_Alarm.this,
						GPS_Alarm.class);
				startActivity(newIntent);
			}
		});

		radius = (EditText) findViewById(R.id.radiusTextId);
		descr = (EditText) findViewById(R.id.descriptionTextId);

		saveButton = (Button) findViewById(R.id.saveButtonId);
		saveButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				String descrString = descr.getText().toString();
				String radiusString = radius.getText().toString();
				Double xCoordDouble = ah.getxTempCoord();
				Double yCoordDouble = ah.getyTempCoord();
				if (ah.getIsLoggedIn() != true) {
					if (xCoordDouble == null || yCoordDouble == null
							|| descrString.isEmpty() || radiusString.isEmpty()) {
						Toast.makeText(
								getBaseContext(),
								"Please enter description, radius and coordinates.",
								Toast.LENGTH_SHORT).show();
					} else {
						ah.setxDestinationCoordinate(xCoordDouble);
						ah.setyDestinationCoordinate(yCoordDouble);
						ah.setRadius(Double.parseDouble(radiusString));
						ah.setDescription(descrString);
						Intent newIntent = new Intent(Set_Point_GPS_Alarm.this,
								GPS_Alarm.class);
						startActivity(newIntent);
					}
				} else {
					if (xCoordDouble == null || yCoordDouble == null
							|| descrString == "" || radiusString == "") {
						Toast.makeText(
								getBaseContext(),
								"Please enter description, radius and coordinates.",
								Toast.LENGTH_SHORT).show();
					} else {
						ah.setxDestinationCoordinate(xCoordDouble);
						ah.setyDestinationCoordinate(yCoordDouble);
						ah.setRadius(Double.parseDouble(radiusString));
						ah.setDescription(descrString);
						Intent newIntent = new Intent(Set_Point_GPS_Alarm.this,
								loggedIn_GPS_Alarm.class);
						startActivity(newIntent);
					}
				}
			}
		});

		MapView mapView = (MapView) findViewById(R.id.mapviewId);
		mapView.setBuiltInZoomControls(true);
		mapView.setSatellite(true);
		MapController mapController = mapView.getController();
		mapController.setZoom(10); // Zoom 1 is world view

		mc = mapView.getController();

		double lat = ah.getxCoordinate(); // Holt Koordinaten aus dem Attribute
											// Handler
		double lng = ah.getyCoordinate();

		p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

		mc.animateTo(p);

		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.red_x);
		ItemizedOverlay_GPS_Alarm itemizedoverlay = new ItemizedOverlay_GPS_Alarm(
				drawable, this);

		int latitude = (int) (lat * 1E6);
		int longitude = (int) (lng * 1E6);

		GeoPoint point = new GeoPoint(latitude, longitude);
		OverlayItem overlayitem = new OverlayItem(point, "Current position", "");

		itemizedoverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedoverlay); // davor die Liste leeren, damit nur
											// ein Punkt vorhanden ist
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * LocationListener setzt die jetztigen Koordinaten in den AttributeHandler
	 * 
	 * @param savedInstanceState
	 *            Standardparameter
	 */
	private LocationListener createLocationListener() {
		locationListener = new LocationListener() {

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
			}

			public void onProviderEnabled(String provider) {
			}

			public void onProviderDisabled(String provider) {
			}

			public void onLocationChanged(Location location) {
				if (location != null) {
					ah.setxCoordinate(location.getLatitude());
					ah.setyCoordinate(location.getLongitude());
				}
			}
		};

		return locationListener;
	}

}